let fs = require('fs')
require('shelljs/global')
let argv = require('yargs').argv
let pwd = process.cwd()
let str = process.argv

const reactReBack = () => {
	if (pwd.indexOf('setting') > 1) {
		cd(`${__dirname}/..`)
	}
	exec(`cp -rf setting/backup/react-scripts react/node_modules/  `)
	exec(`cp -rf setting/backup/react-dev-utils react/node_modules/  `)
}

const reactBackup = () => {
	if (pwd.indexOf('setting') > 1) {
		exec(`rm -rf backup`)
		exec(`mkdir backup`)
		cd(`${__dirname}/..`)
	} else {
		exec(`rm -rf setting/backup`)
		exec(`mkdir setting/backup`)
	}
	exec(`cp -rf react/node_modules/react-scripts  setting/backup/`)
	exec(`cp -rf react/node_modules/react-dev-utils  setting/backup/`)
}
const reactDev = () => {
	if (pwd.indexOf('setting') > 1) {
		cd(`${__dirname}/..`)
	}
	exec(`cp -rf setting/dev/react-scripts react/node_modules/  `)
	exec(`cp -rf setting/dev/react-dev-utils react/node_modules/  `)
	console.log('setting react-scripts react-dev-utils')
}

const reactMoveBuild = () => {
	if (pwd.indexOf('setting') > 1) {
		cd(`${__dirname}/..`)
	}
	exec(`rm -rf /koa/build`)
	exec(`mv /react/build /koa/`)
	exec(`rm -rf /koa/build`)
	console.log('setting react-scripts react-dev-utils')
}

const reactRun = () => {
	if (pwd.indexOf('setting') > 1) {
		cd(`${__dirname}/..`)
	}
	cd(`/react`)
	exec(`npm run start`)
}

if (str[2] === 'react') {
	if (str[3] === 'build') {
		reactMoveBuild()
	}
	else if (str[3] === 'backup') {
		reactBackup()
	}
	else if (str[3] === 'reback') {
		reactReBack()
	}
	else if (str[3] === 'dev') {
		reactDev()
	}
}